<template>
  <div>
    <el-header>
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-header>
    <div>
      <el-input
        class="input"
        v-model="input"
        placeholder="请输入内容"
      ></el-input>
      <el-button
        type="primary"
        icon="el-icon-search"
        @click="search"
      ></el-button>
      <el-button @click="drawerHandle('添加')">添加用户</el-button>
    </div>

    <!-- 渲染 -->
    <el-table :data="list" border style="width: 100%">
      <el-table-column align="center" label="序号" width="50">
        <template slot-scope="{ $index }">
          {{ obj.pagesize * (obj.pagenum - 1) + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="username" label="姓名" width="150">
      </el-table-column>
      <el-table-column
        align="center"
        prop="email"
        label="邮箱"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="mobile"
        label="电话"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="role_name"
        label="角色"
      ></el-table-column>
      <el-table-column align="center" prop="mg_state" label="状态">
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.mg_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="zhuantai(row)"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="300">
        <template slot-scope="{ row }">
          <!-- {{ row }} -->
          <div class="actions">
            <i
              class="el-icon-edit-outline"
              @click="drawerHandle('编辑', row)"
            ></i>
            <i class="el-icon-delete" @click="del(row.id)"></i>
            <el-tooltip
              class="item"
              effect="dark"
              content="分配角色"
              placement="top"
            >
              <i class="el-icon-s-tools" @click="getUserxq(row)"></i>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 插槽 -->
    <Dialog :showFlag.sync="showFlag" :title="title">
      <el-form
        v-if="title == '编辑'"
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            disabled
            type="username"
            v-model="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input
            type="text"
            v-model="ruleForm.email"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input
            type="text"
            v-model="ruleForm.mobile"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-form
        v-else
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="username"
            v-model="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="text"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input
            type="text"
            v-model="ruleForm.mobile"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input
            type="text"
            v-model="ruleForm.email"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>

      <el-button @click="submit">确认</el-button>
      <el-button @click="cancel">关闭</el-button>
    </Dialog>
    <!-- 分配角色请求框 -->
    <el-dialog title="分配角色" :visible.sync="dialogFormVisible">
      当前用户:{{ ruleForm.username }} <br />
      当前角色:{{ ruleForm.role_name }} <br />
      分配新角色:<el-select v-model="region" placeholder="请选择活动区域">
        <el-option label="管理员" value="管理员"></el-option>
        <el-option label="wqare" value="wqare"></el-option>
      </el-select>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="fenpei">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Dialog from "@/components/Dialog.vue"
import { getUserApi, DelUserApi, updateUser, addUserApi, getUserx, xiugaiUser, zhuantaiUser } from '@/api/user'
export default {
  name: 'userList',
  components: {
    Dialog
  },
  data () {
    return {
      title: '',
      showFlag: false,
      obj: {
        pagesize: 1,
        pagenum: 1,
        query: ''
      },
      list: [],
      total: 0,
      mg_state: '',
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur', }
        ],
        email: [
          { required: true, message: '邮件不能为空', trigger: 'blur' },

        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机不能为空', trigger: 'blur' },
        ]
      },
      ruleForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },

      input: "",
      rid: '',
      dialogFormVisible: false,
      dialogTableVisible: false,
      region: '',


    }
  },

  created () {
    this.getUser()

  },
  computed: {
  },
  methods: {
    async getUser () {
      const res = await getUserApi(this.obj)
      // console.log(res)
      this.total = res.data.total
      this.list = res.data.users
    },
    async getUserxq (row) {
      const res = await getUserx(row.id)
      // console.log(res)
      this.ruleForm = row
      this.rid = res.data.rid
      this.dialogFormVisible = true
    },
    async fenpei () {
      try {
        // console.log(this.ruleForm.id,this.rid)

        const res = await xiugaiUser({ id: this.ruleForm.id, rid: this.rid })
        console.log(res, 99999)
        this.$message.error(res.meta.msg)

      } catch (error) {
      }
      // this.dialogFormVisible = false

    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
      this.obj.pagesize = val
      this.getUser()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
      this.obj.pagenum = val
      this.getUser()
    },
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await DelUserApi(id)
        this.getUser()
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    drawerHandle (type, row) {
      if (type == '编辑') {
        this.ruleForm = JSON.parse(JSON.stringify(row))

      }
      this.title = type
      this.showFlag = true

    },
    async submit () {

      if (this.title == '编辑') {
        await updateUser(this.ruleForm)
      } else if (this.title == '添加') {
        const res = await addUserApi(this.ruleForm)
        console.log(res, 2222)

      }
      //关闭
      this.showFlag = false
      // 重新渲染
      this.getUser()
      //清空
      this.ruleForm.username = ''
      // this.ruleForm.password = ''
      this.ruleForm.email = ''
      this.ruleForm.mobile = ''
    },
    cancel () {
      this.showFlag = false
      this.ruleForm = ''
    },
    search () {
      this.obj.query = this.input
      this.getUser()
    },
    async zhuantai (row) {
      const res = await zhuantaiUser({ id: row.id, bool: row.mg_state })
      // console.log(res, 909090)
      this.$message({
        message: '修改状态成功',
        type: 'success'
      });
    }
  }

}
</script>
<style lang='scss'  scoped>
i {
  margin: 0 10px;
}
.input {
  width: 200px;
}
</style>
